package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysApp;
import com.ruoyi.system.mapper.SysAppMapper;
import com.ruoyi.system.service.ISysAppService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author renzhihao
 * @date 2024/04/30 10:24
 */
@Service
@AllArgsConstructor
public class SysAppServiceImpl implements ISysAppService {

    private final SysAppMapper appMapper;

    @Override
    public List<SysApp> selectAppList(SysApp sysApp) {
        return appMapper.selectAppList(sysApp);
    }

    @Override
    public SysApp selectAppById(String appId) {
        return appMapper.selectById(appId);
    }

    @Override
    public int insertApp(SysApp sysApp) {
        sysApp.setCreateBy(ShiroUtils.getLoginName());
        return appMapper.insertApp(sysApp);
    }

    @Override
    public int updateApp(SysApp sysApp) {
        sysApp.setUpdateBy(ShiroUtils.getLoginName());
        return appMapper.updateApp(sysApp);
    }

    @Override
    public int deleteAppByIds(String[] appIds) {
        return appMapper.deleteAuthByIds(appIds);
    }

    @Override
    public int changeStatus(SysApp sysApp) {
        sysApp.setUpdateBy(ShiroUtils.getLoginName());
        return appMapper.updateApp(sysApp);
    }

    @Override
    public boolean checkAppNameUnique(SysApp sysApp) {
        String appId = StringUtils.isNull(sysApp.getAppId()) ? "" : sysApp.getAppId();
        SysApp info = appMapper.checkAppNameUnique(sysApp.getAppName());
        if (StringUtils.isNotNull(info) && !StringUtils.equals(info.getAppId(), appId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkClientIdUnique(SysApp sysApp) {
        String appId = StringUtils.isNull(sysApp.getAppId()) ? "" : sysApp.getAppId();
        SysApp info = appMapper.checkClientIdUnique(sysApp.getClientId());
        if (StringUtils.isNotNull(info) && !StringUtils.equals(info.getAppId(), appId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public SysApp selectAppByClientId(String clientId) {
        return appMapper.checkClientIdUnique(clientId);
    }
}
